﻿using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace NetJsWire.Widgets.Ui.Table
{
    internal class TableColumnCollectionEditor : UITypeEditor
    {
        // Fields
        private TableColumnCollectionDialog dataGridViewColumnCollectionDialog;

        // Methods
        private TableColumnCollectionEditor()
        {
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider != null)
            {
                IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if ((service == null) || (context.Instance == null))
                {
                    return value;
                }
                IDesignerHost host = (IDesignerHost)provider.GetService(typeof(IDesignerHost));
                if (host == null)
                {
                    return value;
                }
                if (this.dataGridViewColumnCollectionDialog == null)
                {
                    this.dataGridViewColumnCollectionDialog = new TableColumnCollectionDialog();
                }
                this.dataGridViewColumnCollectionDialog.SetLiveDataGridView((Table)context.Instance);
                using (DesignerTransaction transaction = host.CreateTransaction("DataGridViewColumnCollectionTransaction"))
                {
                    if (service.ShowDialog(this.dataGridViewColumnCollectionDialog) == DialogResult.OK)
                    {
                        transaction.Commit();
                        return value;
                    }
                    transaction.Cancel();
                }
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }
    }


}
